将Memcached作为服务自动启动

1、最简单的做法

通常:启动Memcache的服务器端的命令为:

/usr/local/bin/memcached -d -m 256 -u root -l 127.0.0.1 -p 12000 -c 2048 -P /tmp/memcached.pid

 在/etc/rc.d/rc.local中加入一行,上面命令 ,最好选择是:nobody 或 deamon

-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB,
-u是运行Memcache的用户,我这里是root,
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.0.200,
-p是设置Memcache监听的端口,我这里设置了12000,最好是1024以上的端口,
-c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定,
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid,

 2、写成脚本,作为服务

#!/bin/sh
#
# memcached    Startup script for memcached processes
#
# chkconfig: - 90 10
# description: Memcache provides fast memory based storage.
# processname: memcached
# Date: 2015-08-08
# Author: cpz@erongtu.com

. /etc/rc.d/init.d/functions
. /etc/sysconfig/network

IPADDR=`/sbin/ifconfig eth0 | awk -F ':' '/inet addr/{print $2}' | sed 's/[a-zA-Z ]//g'`
MEMCACHED="/usr/local/bin/memcached"
PROG=$(basename  $MEMCACHED)
PORT=12000
USER=root
MEMORY=256
PID="/tmp/memcached.pid"
CONN=2048
LOCKFILE="/var/lock/subsys/memcached"
RETVAL=0

[ -x $MEMCACHED ] || exit 0

start() {
    if [ -e $PID -a ! -z $PID ];then
        echo $PROG" already running"
        exit 1
    fi

    echo -n $"Starting $PROG"
    # Starting memcached with 256MB memory on port 12000 as deamon and user nobody
    $MEMCACHED -m $MEMORY -p $PORT -l $IPADDR -d -u $USER  -c $CONN -P $PID
    RETVAL=$?

    [ $RETVAL -eq 0 ] && {
        touch $LOCKFILE
        success $"$PROG"
    }
    echo 

    return $RETVAL
}

stop() {
    echo -n $"Stopping $PROG "

    killproc -d 10 $MEMCACHED
    echo
    [ $RETVAL = 0 ] && rm -f $PID $LOCKFILE

    RETVAL=$?
    return $RETVAL
}

reload() {
    echo -n $"Reloading $PROG"
    killproc $MEMCACHED -HUP

    RETVAL=$?
    echo
    return $RETVAL
}

case "$1" in
    start)
        start
        ;;

    stop)
        stop
        ;;

    reload)
        reload
        ;;

    restart)
        stop
        start
        ;;

    condrestart)
        if test "x`pidof memcached`" != x; then
            stop
            start
        fi
        ;;

    *)
        echo $"Usage: $0 {start|stop|restart|reload|condrestart}"
        exit 1

esac

exit $RETVAL

#chmod +x /etc/init.d/memcached
#chkconfig --add memcached
#chkconfig memcached on

 3、附上 memcached 的安装过程

1)下载(软件不是最新,请到官网下载最新版本)

cd /usr/loca/src
wget http://www.memcached.org/files/memcached-1.4.24.tar.gz
wget https://sourceforge.net/projects/levent/files/libevent/libevent-2.0/libevent-2.0.22-stable.tar.gz

 2)安装libevent

cd /usr/loca/src
tar zxvf libevent-2.0.22-stable.tar.gz
cd libevent-2.0.22-stable
./configure --prefix=/usr/local/libevent
make && make install

3) 测试libevent

ls -al /usr/lib | grep libevent

 4)安装memcached

cd /usr/loca/src
tar zxvf memcached-1.4.24.tar.gz
cd memcached-1.4.24
./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/

make && make install

 5)测试是否安装成功

ls -al /usr/local/bin/mem*

 6)安装libmemcached

cd /usr/local/src
wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
tar zxvf libmemcached-0.42.tar.gz
cd libmemcached-0.42
./configure --prefix=/usr/local/libmemcached  --with-memcached
make && make install

7)PHP安装memcached扩展

pecl install memcached

 或者

cd /usr/local/src
wget https://pecl.php.net/get/memcached-2.2.0.tgz
tar zxf memcached-2.2.0.tgz
cd memcached-2.2.0
phpize
./configure --enable-memcached  --with-libmemcached-dir=/usr/local/libmemcached
make && make install
echo extension=memcached.so > /etc/php.d/memcached.ini
service php-fpm reload

 

posted @ 2015-09-08 16:22  踏雪无痕SS  阅读(853)  评论(0编辑  收藏  举报